﻿using HZY.Framework.DependencyInjection;
using IoTClient.Clients.PLC;
using Microsoft.Extensions.Logging;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;

namespace Long.SpraySCADA
{
    public partial class PageTotalEquipmentControl : UIPage, ISingletonSelfDependency
    {

        //调用注入的 日志 类
        private readonly ILogger<PageTotalEquipmentControl> _logger;

        public PageTotalEquipmentControl(ILogger<PageTotalEquipmentControl> _logger)
        {
            InitializeComponent();
            this._logger = _logger;
            LogExtension.ShowMessage = ShowLog;
        }

        private void btn_Control_Common_Click(object sender, EventArgs e)
        {
            UISymbolButton uiButton = sender as UISymbolButton;

            if (!Globals.SiemensClient.Connected)
            {
                UIMessageTip.ShowError("请先连接到西门子PLC");
                return;
            }

            if (Globals.PLCWrite(uiButton.TagString, true))
            {
                UIMessageTip.ShowOk("写入成功");
            }
            else
            {
                UIMessageTip.ShowError($"写入 {uiButton.TagString} 失败");
            }
        }

        private void btn_DryRun_ClickEvent(object sender, EventArgs e)
        {
            if (!Globals.SiemensClient.Connected)
            {
                UIMessageTip.ShowError("请先连接到西门子PLC");
                return;
            }

            //点击取反
            if(Globals.PLCWrite(this.btn_DryRun.VariableName, !this.btn_DryRun.ButtonCounterStatus))
            {
                //改变按钮状态
                this.btn_DryRun.ButtonCounterStatus =!this.btn_DryRun.ButtonCounterStatus;
                UIMessageTip.ShowOk($"{this.btn_DryRun.VariableName}:改变空运行状态");
            }
            else
            {
                UIMessageTip.ShowError($"{this.btn_DryRun.VariableName}:未改变空运行状态");
            }
        }

        private void device_Common_ClickEvent(object sender, EventArgs e)
        {
            UserDeviceUnitControl userDeviceUnit = sender as UserDeviceUnitControl;
            if (!Globals.SiemensClient.Connected)
            {
                userDeviceUnit.State = false;
                UIMessageTip.ShowError("请先连接PLC");
                return;
            }

            bool state = userDeviceUnit.State;
            string variableName = state ? userDeviceUnit.CloseVariableName : userDeviceUnit.OpenVariableName;
            if (Globals.PLCWrite(variableName, state))
            {
                UIMessageTip.ShowOk($"{userDeviceUnit.EquipmentUnitName}:改变状态成功");
                ShowLog($"{userDeviceUnit.EquipmentUnitName}:改变状态成功");
            }
            else
            {
                UIMessageTip.ShowError($"{userDeviceUnit.EquipmentUnitName}:改变状态失败");
            }
        }

        private void ShowLog(string log,LogLevel logLevel=LogLevel.Information)
        {
            //面板显示日志
            this.txt_Log.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:dd:ss") + "-->" + log + Environment.NewLine);

            //保存日志
            switch (logLevel)
            {           
                case LogLevel.Information:
                    _logger.LogInformation(log);
                    break;
                case LogLevel.Warning:
                    _logger.LogWarning(log);
                    break;
                case LogLevel.Error:
                    _logger.LogError(log);
                    break;
                default:
                    break;
            }
        }
        #region 窗体切换卡顿
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams paras = base.CreateParams;
                paras.ExStyle |= 0x02000000;
                return paras;
            }
        }
        #endregion
    }
}
